//
//  HYFTabBar.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/19.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit

class HYFTabBar: UITabBar {
    
    //声明一个闭包
    var closure:(()->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        //添加控件
        addSubview(btn_compose)
    }
    
    //设置子控件的 frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = frame.width * 0.2
        var index:CGFloat = 0
        
       
        //遍历子控件
        for value in subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                value.frame.origin.x = w * index
                value.frame.size.width = w
                
                index++
            }
            
            if index == 2 {
                
                index++
            }
        }
        
        btn_compose.center.x = frame.size.width * 0.5
        btn_compose.center.y = frame.size.height * 0.5
    }
    
    //点击 tabBar 按钮
    func buttonClick(){
    closure?()
    }
    
    //懒加载控件
    lazy var btn_compose:UIButton = {
        
       let button = UIButton()
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        //添加点击事件
        button.addTarget(self, action: #selector(HYFTabBar.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置大小(size)
        button.sizeToFit()
        
        return button
    }()

}
